/*
 * Communication.c
 *
 *  Created on: 01-10-2011
 *      Author: Jarek
 */
#include <htc.h>
#include "Typedefs.h"
#define CO_Preambule1	0x2D
#define CO_Preambule2	0xD2
#define CO_DeviceID	0x01

enum{
	CO_ReadPreambule1 = 0,
	CO_ReadPreambule2,
	CO_ReadDeviceID,
	CO_ReadCommandNr,
	CO_ReadRegister,
	CO_ReadCRC
};


enum{
	CO_TransStop = 0,
	CO_TransPreambule1,
	CO_TransPreambule2,
	CO_TransDeviceID,
	CO_TransCommandNr,
	CO_TransData,
	CO_TransCRC,

};

/* Zmienne sterujace transmisja. */
uchar uc_RecBuf[20];//Bufor odebranych danych
uchar uc_TransBuf[20];//Bufor wyslanych danych
uchar uc_TransmitTask = CO_TransStop;
uchar uc_RecTask = CO_ReadPreambule1;
uchar uc_TransCnt = 0;
uchar uc_RecCnt = 0;



extern ushort un_ObrotySrednia;

extern ushort un_EEP_ProgObroty;
extern ushort un_EEP_ProgCisnienie;
extern ushort un_EEP_SetTime;
extern ushort un_EEP_CutOffObroty;
extern uchar uc_EEP_CutOffSet;
extern uchar uc_EEP_ObrotyMnoznik;
extern uchar un_StanWyjsc;

extern ushort un_CisnienieSrednia;
void fv_SendRegister(uchar uc_RegNr)
{

	if(uc_RecBuf[0] == CO_DeviceID)
	{


	uc_TransmitTask = CO_TransPreambule1;
	uc_TransBuf[0] = CO_DeviceID;
	uc_TransBuf[1] = uc_RegNr;
	uc_TransBuf[2] = 0;
	uc_TransBuf[3] = 0;
	uc_TransBuf[4] = 0;
	uc_TransBuf[5] = 0;
	switch(uc_RegNr)
	{
		case 1://cisnienie/obroty
		{
			uc_TransBuf[2] = un_CisnienieSrednia >> 8;
			uc_TransBuf[3] = un_CisnienieSrednia & 0xFF;
			uc_TransBuf[4] = un_ObrotySrednia >> 8;
			uc_TransBuf[5] = un_ObrotySrednia & 0xFF;

		}break;
		case 2://progi
		{
			uc_TransBuf[2] = un_EEP_ProgCisnienie >> 8;
			uc_TransBuf[3] = un_EEP_ProgCisnienie & 0xFF;
			uc_TransBuf[4] = un_EEP_ProgObroty >> 8;
			uc_TransBuf[5] = un_EEP_ProgObroty & 0xFF;
		}break;
		case 3://odciecie
		{
			uc_TransBuf[2] = un_EEP_CutOffObroty >> 8;
			uc_TransBuf[3] = un_EEP_CutOffObroty & 0xFF;
			uc_TransBuf[4] = uc_EEP_CutOffSet;
			uc_TransBuf[5] = 0x00;
		}break;
		case 4://sterowanie
		{
			uc_TransBuf[2] = un_EEP_SetTime >> 8;
			uc_TransBuf[3] = un_EEP_SetTime & 0xFF;
			uc_TransBuf[4] = uc_EEP_ObrotyMnoznik;
			uc_TransBuf[5] = un_StanWyjsc;
		}break;




		case 104://sterowanie
		{
			un_EEP_SetTime = uc_RecBuf[2];
			un_EEP_SetTime <<= 8;
			un_EEP_SetTime |= uc_RecBuf[3];
			uc_EEP_ObrotyMnoznik = uc_RecBuf[4];
			un_StanWyjsc |= uc_RecBuf[5];
		}break;

	}

	}
	TXIE = 1;
	TXREG = 0xFF;

}
//
//void fv_GetRegister(uchar uc_RegNr)
//{
//
//	uc_TransmitTask = CO_TransPreambule1;
//	uc_TransBuf[0] = CO_DeviceID;
//	uc_TransBuf[1] = uc_RegNr;
//
//	switch(uc_RegNr)
//	{
//		case 1://cisnienie/obroty
//		{
//			uc_TransBuf[2] = un_CisnienieSrednia >> 8;
//			uc_TransBuf[3] = un_CisnienieSrednia & 0xFF;
//			uc_TransBuf[4] = un_ObrotySrednia >> 8;
//			uc_TransBuf[5] = un_ObrotySrednia & 0xFF;
//
//		}break;
//		case 2://progi
//		{
//			uc_TransBuf[2] = un_EEP_ProgCisnienie >> 8;
//			uc_TransBuf[3] = un_EEP_ProgCisnienie & 0xFF;
//			uc_TransBuf[4] = un_EEP_ProgObroty >> 8;
//			uc_TransBuf[5] = un_EEP_ProgObroty & 0xFF;
//		}break;
//		case 3://odciecie
//		{
//			uc_TransBuf[2] = un_EEP_CutOffObroty >> 8;
//			uc_TransBuf[3] = un_EEP_CutOffObroty & 0xFF;
//			uc_TransBuf[4] = uc_EEP_CutOffSet;
//			uc_TransBuf[5] = 0x00;
//		}break;
//		case 4://sterowanie
//		{
//			uc_TransBuf[2] = un_EEP_SetTime >> 8;
//			uc_TransBuf[3] = un_EEP_SetTime & 0xFF;
//			uc_TransBuf[4] = uc_EEP_ObrotyMnoznik;
//			uc_TransBuf[5] = un_StanWyjsc;
//		}break;
//	}
//
//	TXIE = 1;
//	TXREG = 0xFF;
//
//}
void fv_RecData(void)
{

	static uchar uc_Crc = 0;
	static uchar uc_Cnt = 0;
	static uchar uc_Type = 0;
	uchar uc_Buf;

	uc_Buf = RCREG;
	uc_Crc += uc_Buf;
	switch(uc_RecTask)
	{
		case CO_ReadPreambule1:
		{
			if(uc_Buf == CO_Preambule1)
			{
				uc_RecTask = CO_ReadPreambule2;
			}
		}break;
		case CO_ReadPreambule2:
		{
			if(uc_Buf == CO_Preambule2)
			{
				uc_RecCnt = 0;
				uc_Crc = 0;
				uc_RecTask = CO_ReadDeviceID;
			}
		}break;
		case CO_ReadDeviceID:
		{
			if(uc_Buf == CO_DeviceID)
			{
				uc_RecBuf[uc_RecCnt] = uc_Buf;
				uc_RecCnt++;
				uc_RecTask = CO_ReadCommandNr;
			}
		}break;
		case CO_ReadCommandNr:
		{
			if(uc_Buf == 0xFF)
			{
				uc_RecBuf[uc_RecCnt] = uc_Buf;
				uc_RecCnt++;
				uc_Cnt = 1;
				uc_Type = 0;
				uc_RecTask = CO_ReadRegister;
			}
			else
			{
				uc_RecBuf[uc_RecCnt] = uc_Buf;
				uc_RecCnt++;
				uc_Cnt = 4;
				uc_Type = 1;
				uc_RecTask = CO_ReadRegister;

			}
		}break;
		case CO_ReadRegister:
		{
			if(uc_Cnt > 0)
			{
				uc_Cnt--;
				uc_RecBuf[uc_RecCnt] = uc_Buf;
				uc_RecCnt++;
			}
			else
			{
				if(uc_Crc == 0)
				{
					if(uc_Type == 1)
					{
						fv_SendRegister(uc_RecBuf[1]);
					}
					else
					{
						fv_SendRegister(uc_RecBuf[2]);
					}
				}
				uc_RecTask = CO_ReadPreambule1;
			}
		}break;
		case CO_ReadCRC:
		{/* CRC*/

		}break;
	}

}



void fv_TransmitData(void)
{

	static uchar uc_Crc = 0;
	uchar uc_Buf;

	switch(uc_TransmitTask)
	{
		case CO_TransStop:
		{
			TXIE = 0;
			return;
		}break;
		case CO_TransPreambule1:
		{
			uc_Buf = CO_Preambule1;
			uc_TransmitTask = CO_TransPreambule2;
		}break;
		case CO_TransPreambule2:
		{
			uc_Buf = CO_Preambule2;
			uc_TransCnt = 0;
			uc_Crc = 0;
			uc_TransmitTask = CO_TransData;
		}break;
		case CO_TransData:
		{
			if(uc_TransCnt < 6)
			{
			uc_Buf = uc_TransBuf[uc_TransCnt];
			uc_Crc += uc_Buf;
			uc_TransCnt++;
			}
			else
			{
				uc_Buf = 0x100 - uc_Crc;
				uc_TransmitTask = CO_TransStop;
			}
		}break;
	}
	TXREG = uc_Buf;
}
